package com.xiaochen.baidu.service.node;

import com.xiaochen.baidu.common.design.strategy.AbstractMultiThreadStrategyRouter;
import com.xiaochen.baidu.common.design.strategy.StrategyHandler;
import com.xiaochen.baidu.model.entity.RequestEntity;
import com.xiaochen.baidu.service.factory.DefaultStrategyFactory;
import com.xiaochen.baidu.service.thread.ClassThreadTask;
import com.xiaochen.baidu.service.thread.ParseThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * @author xc
 * @date 2025/10/11/22:41
 * @desc
 */
@Slf4j
@Service
public class ParseNode extends AbstractMultiThreadStrategyRouter<RequestEntity, DefaultStrategyFactory.StrategyDynamicContext, String> {
    @Override
    protected void multiThread(RequestEntity requestParameter, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        ClassThreadTask classThreadTask = new ClassThreadTask(strategyDynamicContext.getSurl(), strategyDynamicContext.getPwd(), "", strategyDynamicContext.getDir(),strategyDynamicContext.getNum(), moiuParseService);
        CompletableFuture<String> classFuture = CompletableFuture.supplyAsync(classThreadTask, threadPoolExecutor);
        String classResult = classFuture.join();
        log.info("获取dir等信息:{}", classResult);
        //dir:uk:shareId:randsk:fsId:server_filename:size
        String[] split = classResult.split(":");
        if (!split[0].equals(strategyDynamicContext.getDir())){
            strategyDynamicContext.setDir(split[0]);
            //缓存存储dir
            redisService.setValue("baidu:" + strategyDynamicContext.getSurl(), strategyDynamicContext.getPwd() + ":" + strategyDynamicContext.getDir());
        }
        strategyDynamicContext.setUk(Long.parseLong(split[1]));
        strategyDynamicContext.setShareId(Long.parseLong(split[2]));
        strategyDynamicContext.setRandsk(split[3]);
        strategyDynamicContext.setFsId(Long.parseLong(split[4]));
        strategyDynamicContext.setServerFilename(split[5]);
        strategyDynamicContext.setSize(Long.parseLong(split[6]));
        ParseThreadTask parseThreadTask = new ParseThreadTask(strategyDynamicContext.getSurl(), strategyDynamicContext.getUk(), strategyDynamicContext.getShareId(), strategyDynamicContext.getRandsk(), strategyDynamicContext.getFsId(), strategyDynamicContext.getServerFilename(), strategyDynamicContext.getSize(), strategyDynamicContext.getKdownSession(), moiuParseService);
        CompletableFuture<String> parseFuture = CompletableFuture.supplyAsync(parseThreadTask, threadPoolExecutor);
        String parseResult = parseFuture.join();
        log.info("获取dlink:{}", parseResult);
        strategyDynamicContext.setDlink(parseResult);
    }

    @Override
    protected String apply(RequestEntity requestParameter, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        return strategyDynamicContext.getDlink();
    }

    @Override
    public StrategyHandler<RequestEntity, DefaultStrategyFactory.StrategyDynamicContext, String> map(RequestEntity requestEntity, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        return null;
    }
}
